package com.fly.control;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.fly.model.Stock;
import com.fly.conn.*;
public class StockControl {
	public static boolean insertStock(Stock s){
		String sql = "insert into stock(name,barcode, standard, unit, classify, supplier, nums, stockprice, stocktime)"+
		"values(?, ?, ?, ?, ?, ?, ?, ?, ?)";
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		try{
			int i = 1;
			dbc.setString(i++, s.getName());
			dbc.setString(i++,s.getBarcode());
			dbc.setString(i++, s.getStandard());
			dbc.setInt(i++, s.getUnit());
			dbc.setInt(i++, s.getClassify());
			dbc.setInt(i++, s.getSupplier());
			dbc.setInt(i++, s.getNums());
			dbc.setString(i++, s.getStockPrice());
			dbc.setString(i++, s.getStockTime());
			dbc.executeUpdate();
			return true;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("调用函数insertStock，插入进货信息失败");
			return false;
		} finally{
			dbc.close();
		}
	}
	public  static boolean deleteStock(int id){
		String sql = "delete * from stock where id = ?";
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		try{
			dbc.setInt(1, id);
			dbc.executeUpdate();
			return true;
		} catch(Exception e){
			System.out.println("调用deleteStock函数失败");
			e.printStackTrace();
		} finally{
			dbc.close();
		}
		return false;
	}
	public static boolean updateStock(Stock s,int id){
		String sql = "update stock set name = ?, barcode = ?, standard = ?, unit = ?, classify =? , supplier = ?, nums = ?,  stockprice = ?, stocktime = ? where id = ?";
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		try{
			int i = 1;
			dbc.setString(i++,s.getName());
			dbc.setString(i++, s.getBarcode());
			dbc.setString(i++,s.getStandard());
			dbc.setInt(i++,s.getUnit());
			dbc.setInt(i++,s.getClassify());
			dbc.setInt(i++,s.getSupplier());
			dbc.setInt(i++,s.getNums());
			dbc.setString(i++,s.getStockPrice());
			dbc.setString(i++,s.getStockTime());
			dbc.setInt(i++,id);
			return true;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("调用updateStock函数失败");
		} finally{
			dbc.close();
		}
		return false;
	}
	public static Stock getStock(int id){
		String sql = "select * from stock where id = ?";
		DBConnect dbc = new DBConnect();
		ResultSet rs = null;
		dbc.prepareStatement(sql);
		try{
			dbc.setInt(1, id);
			rs = dbc.executeQuery();
			Stock s = new Stock();
			while(rs.next()){	
				s.setName(rs.getString("name"));
				s.setBarcode(rs.getString("barcode"));
				s.setStandard(rs.getString("standard"));
				s.setUnit(rs.getInt("unit"));
				s.setClassify(rs.getInt("classify"));
				s.setSupplier(rs.getInt("supplier"));
				s.setNums(rs.getInt("nums"));
				s.setStockPrice(rs.getString("stockprice"));
				s.setStockTime(rs.getString("stocktime"));
			}
			return s;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("调用getStock失败");
		} finally{
			dbc.close();
		}
		return null;
	}
	public static List<Stock> getStockByName(String name){
		String sql = "select * from stock where name = ?";
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		ResultSet rs = null;
		List<Stock> list = new ArrayList<Stock>();
		try{
			dbc.setString(1, name);
			rs = dbc.executeQuery();
			while(rs.next()){
				Stock s = new Stock();
				s.setName(rs.getString("name"));
				s.setBarcode(rs.getString("barcode"));
				s.setStandard(rs.getString("standard"));
				s.setUnit(rs.getInt("unit"));
				s.setClassify(rs.getInt("classify"));
				s.setSupplier(rs.getInt("supplier"));
				s.setNums(rs.getInt("nums"));
				s.setStockPrice(rs.getString("stockprice"));
				s.setStockTime(rs.getString("stocktime"));
				list.add(s);
			}
			return list;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("调用getStockByName函数失败");
		} finally{
			dbc.close();
		}
		return null;
	}
	public static List<Stock> getStockByBarCode(String barCode){
		String sql = "select * from stock where barcode = ?";
		DBConnect dbc = new DBConnect();
		dbc.prepareStatement(sql);
		ResultSet rs = null;
		List<Stock> list = new ArrayList<Stock>();
		try{
			dbc.setString(1, barCode);
			rs = dbc.executeQuery();
			while(rs.next()){
				Stock s = new Stock();
				s.setName(rs.getString("name"));
				s.setBarcode(rs.getString("barcode"));
				s.setStandard(rs.getString("standard"));
				s.setUnit(rs.getInt("unit"));
				s.setClassify(rs.getInt("classify"));
				s.setSupplier(rs.getInt("supplier"));
				s.setNums(rs.getInt("nums"));
				s.setStockPrice(rs.getString("stockprice"));
				s.setStockTime(rs.getString("stocktime"));
				list.add(s);
			}
			return list;
		} catch(Exception e){
			e.printStackTrace();
			System.out.println("调用getStockByName函数失败");
		} finally{
			dbc.close();
		}
		return null;
	}
}










